package ar.com.personalTravellers.generators;

import ar.com.personalTravellers.beans.Country;
import ar.com.personalTravellers.generators.util.ResourcesDataGeneratorsUtil;
import ar.com.personalTravellers.rowProcessors.CountryRowProcessorCommand;
import ar.com.personalTravellers.spring.ApplicationContextSpring;
import com.angel.dao.generic.interfaces.GenericDAO;
import com.angel.data.generator.exceptions.DataGeneratorException;
import com.angel.data.generator.impl.ImportFileDataGenerator;
import com.angel.data.generator.interfaces.DataGenerator;
import com.angel.data.generator.util.StreamResourcesUtil;
import com.angel.io.descriptor.FileProcessorDescriptor;
import com.angel.io.processors.impl.ExcelFileProcessorCommand;
import com.angel.io.processors.log.ColumnSeparator;
import com.angel.io.processors.runner.impl.ImportFileProcessorRunner;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.Serializable;

/**
 *
 * @author William
 */
public class CountryFileDataGenerator extends ImportFileDataGenerator{

    @SuppressWarnings("unchecked")
	public Class<? extends DataGenerator>[] getDependecesGenerators() {
        return (Class<? extends DataGenerator>[]) new Class[]{};
    }

    public ImportFileProcessorRunner prepareImportFileProcessorRunner() {
		FileProcessorDescriptor descriptor = new FileProcessorDescriptor("Countries excel file.", ColumnSeparator.COMMA);
		ImportFileProcessorRunner runner = new ImportFileProcessorRunner(descriptor,new ExcelFileProcessorCommand(), new CountryRowProcessorCommand());
        runner.addAliasFor(CountryRowProcessorCommand.NAME_COLUMN,"Pais");
        runner.addAliasFor(CountryRowProcessorCommand.LOCALE_COLUMN,"Locale");
        runner.addAliasFor(CountryRowProcessorCommand.CONTINENT_COLUMN,"Continente");
        runner.addAliasFor(CountryRowProcessorCommand.ABBREVIATION_COLUMN,"Breviatura");
        return runner;
    }

    public InputStream prepareInputStream() {
        try {
            return StreamResourcesUtil.findInputStreamFor(ResourcesDataGeneratorsUtil.COUNTRIES_XLS_RESOURCE);
        } catch (FileNotFoundException e) {
            throw new DataGeneratorException("File not found [" + ResourcesDataGeneratorsUtil.COUNTRIES_XLS_RESOURCE + "].", e);
        }
    }

    public Class<? extends Object> getDataGeneratorClass() {
        return Country.class;
    }

    @SuppressWarnings("unchecked")
	public GenericDAO<Object, Serializable> getGenericDAO() {
        return (GenericDAO<Object, Serializable>) ApplicationContextSpring.createInstance().prepareApplicationContext().getBean("countryDAO");
    }
}